// pages/secondPublish/secondPublish.js
var app = getApp();
var model = require('../../utils/model/model.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    backupA: [],
    phone: '',
    canSubmit: false,
    submitData:{
      userId: '', //	用户id			
      title: '', //	标题			
      describtion: '', //	描述			
      price: '', //	价格			
      backupA: '', //	图片url
      backupB: ''
    },
    modelData: {
      showModal: false,
      signle: true,
      signleBtnTxt: "确定",
      content: '',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['submitData.userId']: JSON.parse(wx.getStorageSync('userInfo')).id,
      phone: JSON.parse(wx.getStorageSync('userInfo')).phone,
      ['submitData.backupB']: JSON.parse(wx.getStorageSync('userInfo')).phone
    })
    model.init.apply(this, ['modelData']);
  },
  inputFunc: function(e){
    var key = e.currentTarget.dataset.key;
    let types = e.currentTarget.dataset.type;
    types == 'digit' ? this.data.submitData[key] = e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : this.data.submitData[key] = e.detail.value;
    this.setData({
      submitData: this.data.submitData
    })
    var item = this.data.submitData;
    if (item.title && item.describtion && item.price){
      this.setData({
        canSubmit: true
      })
    }
  },
  checkReg: function (data) {
    var flag = true;
    var reg = new RegExp("^1[34578][0-9]{9}$");
    if (!reg.test('' + this.data.submitData.backupB)) {
      flag = false;
      this.setData({
        'modelData.showModal': true,
        'modelData.content': '请输入正确的手机号'
      });
      return;
    }
    return flag;
  },
  submit: function () {
    if (this.data.canSubmit){
      if (this.checkReg(this.data.submitData.backupB) == true) {
        var _this = this;
        wx.request({
          url: app.globalData.httpApiCar + 'secondHandInfo/save',
          data: _this.data.submitData,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.success) {
              wx.navigateBack()
            }
            else {
              wx.showModal({
                title: '提示',
                content: res.data.error,
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateBack()
                  }
                }
              })
            }
          }
        })
      }
      
    }
  },
  // 添加图片
  addPhoto: function (e) {
    var that = this,
      count = 0,
      key = e.currentTarget.dataset.id,
      only = e.currentTarget.dataset.only;
    only ? count = 1 : count = 9 - this.data[key].length;
    wx.chooseImage({
      count: count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        that.uploadImg(imgsrc, key, 0)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /*上传图片*/
  uploadImg: function (path, key, tag, oIndex) {
    var data1 = app.uploadimg({
      url: app.globalData.httpApiCar + 'uploadfile/upload1',
      path: path,
      globalkey: this,
      realpath: this.data[key],
      showkey: key,
      key: 'submitData.' + key,
      tag: tag,
      oIndex: oIndex
    });
  },
  // 修改图片
  changePhoto: function (e) {
    var that = this,
      key = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.value;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        that.uploadImg(imgsrc, key, 1, index)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  delImg: function (e) {
    var key = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.value;
    this.data[key].splice(index, 1);
    this.setData({
      [key]: this.data[key],
      ['submitData.' + key]: this.data[key].join(",")
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})